import logging
import unittest
import requests
import utils
from parameterized import parameterized
from api.login import LoginApi
from api.tender import TenderApi


class TestTenderApi(unittest.TestCase):

    def setUp(self) -> None:
        self.session = requests.session()
        self.login_api = LoginApi(self.session)
        self.login_api.login("13203003591", "hyc123456")
        self.tender_api = TenderApi(self.session)

    def tearDown(self) -> None:
        self.session.close()

    @parameterized.expand(utils.read_file_freedom("tender", "desc,id,amount,status,status_code"))
    def test01_tender(self, desc, id, amount, status_code, status):
        # 调用投资接口
        res_tender = self.tender_api.get_tender(id, amount)

        # 公共断言
        utils.common_assert(self, res_tender, status_code, status, None)

        # 输出日志
        logging.info(f"投资的结果为：{res_tender.text}")

    def test02_tender_third(self):
        # 调用投资接口
        res_tender = self.tender_api.get_tender(2059, 500)
        # 提取相关数据
        html = res_tender.json()["description"].get("form")
        actions, form_data = utils.parse_html(html)

        # 调用第三方投资接口
        res_tender_third = self.session.post(url=actions, data=form_data)

        # 公共断言
        utils.common_assert(self, res_tender, 200, 200, None)
        self.assertEqual(200, res_tender_third.status_code)
        self.assertIn("OK", res_tender_third.text)

        # 输出日志
        logging.info(f"投资的结果为：{res_tender.text}")
        logging.info(f"第三方投资的结果为：{res_tender_third.text}")
